﻿///***************************************************************
/// 시  스  템 : Updms.Mny
/// 단위시스템 : MNY
/// 계      층 : Presentation Layer
/// 프로그램명 : Mny2014r
/// 파  일  명 : Mny2014r.aspx.cs
/// 설      명 : 주요지출챠트
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-08-22 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;

using Updms.Mny.Exe;
using Updms.Common.BaseClass;


namespace Updms.Mny
{
	public partial class Mny2014r : PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Mny2014r";
		public static readonly string gs_path_info = "/updms/Mny/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Com com = new CommCode.Com(LANG_SET);

		//검색
		protected string kw1 = "";	// 기준년월(시작)
		protected string kw2 = "";	// 기준년월(종료)
		protected string kw3 = "";	//챠트넓이

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetAccount();
			SetYearMonth();
			SetChartWidth();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion	

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";			

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_from_year.SelectedValue + ddl_search_from_month.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_from_year.SelectedValue = kw1.Substring(0, 4).ToString();
						ddl_search_from_month.SelectedValue = kw1.Substring(4, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_to_year.SelectedValue + ddl_search_to_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_to_year.SelectedValue = kw2.Substring(0, 4).ToString();
						ddl_search_to_month.SelectedValue = kw2.Substring(4, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_chart_width.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_chart_width.SelectedValue = kw3;
					}
					break;
				case "SEARCH":
					kw1 = ddl_search_from_year.SelectedValue + ddl_search_from_month.SelectedValue;
					kw2 = ddl_search_to_year.SelectedValue + ddl_search_to_month.SelectedValue;
					kw3 = ddl_search_chart_width.SelectedValue;
					break;
			}
		}		

		#endregion

		#region 마스터 Method (SetAccountv, SetYearMonth, SetChartWidth)

		// 구분
		private void SetAccount()
		{
			DataSet lo_ds = new DataSet();

			lo_ds.Merge(global.GetCommCd("MNY", "AK", LANG_SET));
			lo_ds.Merge(global.GetCommCd("MNY", "AL", LANG_SET));

			cbl_search_account.Items.Clear();
			cbl_search_account.DataSource = lo_ds;
			cbl_search_account.DataMember = "UPDMS_COM_CD";
			cbl_search_account.DataTextField = "Text";
			cbl_search_account.DataValueField = "Value";
			cbl_search_account.DataBind();

			if (cbl_search_account.Items.Count > 0)
			{
				for (int i=0;i<cbl_search_account.Items.Count; i++)
				{
					cbl_search_account.Items[i].Selected = true;
				}
			}
		}

		// 검색조건의 년월설정
		private void SetYearMonth()
		{
			ddl_search_from_year.Items.Clear();
			ddl_search_to_year.Items.Clear();

			int li_f_year = Convert.ToInt32(SEARCH_Y_MIN);
			int li_t_year = Convert.ToInt32(DateUtil.GetYearEx(1, "yyyy"));			
			for (int i = li_f_year; i <= li_t_year; i++)
			{
				string ls_year = i.ToString();
				ddl_search_from_year.Items.Add(new ListItem(ls_year, ls_year));
				ddl_search_to_year.Items.Add(new ListItem(ls_year, ls_year));
			}

			SetYearMonth(-12);
		}
		
		// 검색조건의 년월설정 - 기간지정
		private void SetYearMonth(int li_term)
		{
			ddl_search_from_year.SelectedValue = DateUtil.GetMonthEx(li_term, "yyyy"); //x개월전년도
			ddl_search_from_month.SelectedValue = DateUtil.GetMonthEx(li_term, "MM"); //x개월전월
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}

		//챠트넓이
		private void SetChartWidth()
		{
			com.SetChartWidth(ddl_search_chart_width);
			ddl_search_chart_width.SelectedValue = CHART_RESOLUTION;
		}

		#endregion

		#region 챠트 (DrawImportantOutList, SearchProcess)

		//주요지출항목챠트
		private void DrawImportantOutList()
		{
			int li_line_cnt = 0; // 그래프 라인수		
			bool lb_checked = true;

			//초기화
			chart_important_out.Data.Clear();
			
			//시리즈 수
			chart_important_out.Data.Series = WebControlUtil.GetCheckedItemsCount(cbl_search_account);

			//넓이
			chart_important_out.Width = Convert.ToInt32(kw3);

			for (int i = 0; i < cbl_search_account.Items.Count; i++)
			{
				if (cbl_search_account.Items[i].Selected == true)
				{
					lb_checked = false;
					
					using (Mny2014r_NT _Mny2014r_NT = new Mny2014r_NT())
					{
						DataSet lo_ds = new DataSet();
						DataPack lo_dp = new DataPack();

						lo_dp.AddProperty("ls_base_dt_from", typeof(string), kw1);
						lo_dp.AddProperty("ls_base_dt_to", typeof(string), kw2);
						lo_dp.AddProperty("ls_cd", typeof(string), cbl_search_account.Items[i].Value);
						
						lo_ds.Merge(_Mny2014r_NT.GetImportantOutList(lo_dp));

						int li_count = DataSetUtil.GetRowCount(lo_ds, "UPDMS_MNY_REPORT");

						for (int j = 0; j < li_count; j++)
						{
							//수동으로 데이터 바인딩
							chart_important_out.Data[li_line_cnt, j] = DataSetUtil.GetFldInt(lo_ds, "UPDMS_MNY_REPORT", j, "Amount");

							//X축 라벨링
							chart_important_out.AxisX.Labels[j]
								= "'" + DataSetUtil.GetFldStr(lo_ds, "UPDMS_MNY_REPORT", j, "Base_Dt").Replace("-", ".").Substring(2, 5);

							//0은 안보여준다
							chart_important_out.Points[li_line_cnt, j].PointLabels.Visible = (chart_important_out.Data[li_line_cnt, j] > 0);
						}

						//시리즈
						chart_important_out.Series[li_line_cnt].Text = cbl_search_account.Items[i].Text;
						chart_important_out.Series[li_line_cnt].MarkerShape = ChartFxUtil.markerShape(li_line_cnt);
						li_line_cnt = li_line_cnt + 1;
					}//using
				}//if
			}//for

			// 선택한 지출구분이 없으면
			if (lb_checked)
			{
				gs_page_mode = "";
			}
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			DrawImportantOutList();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":		// 처음 페이지로드시
					table_chart.Visible = false;	// 라인그래프
					break;
				case "R":	// 검색버튼 클릭시
					table_chart.Visible = true;	// 라인그래프
					break;
			}
		}

		#endregion

		#region 버튼이벤트

		//6개월
		protected void sbtn_6_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-5);
		}

		//12개월
		protected void sbtn_12_m_Click(object sender, EventArgs e)
		{
			SetYearMonth(-12);
		}

		//전체
		protected void sbtn_all_m_Click(object sender, EventArgs e)
		{
			ddl_search_from_year.SelectedIndex = 0;
			ddl_search_from_month.SelectedIndex = 0;
			ddl_search_to_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 현재년도
			ddl_search_to_month.SelectedValue = DateUtil.GetMonthEx(0, "MM"); // 현재월
		}

		//챠트
		protected void ibtn_chart_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}		

		#endregion

	}
}
